{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-28T17:39:50.624170Z",
     "start_time": "2020-04-28T17:13:46.042656Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_train shape: (50000, 32, 32, 3)\n",
      "50000 train samples\n",
      "10000 test samples\n",
      "WARNING:tensorflow:From /root/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "If using Keras pass *_constraint arguments to layers.\n",
      "WARNING:tensorflow:From /root/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:4070: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.\n",
      "\n",
      "Using real-time data augmentation.\n",
      "WARNING:tensorflow:From /root/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:422: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.\n",
      "\n",
      "Epoch 1/100\n",
      "1563/1563 [==============================] - 17s 11ms/step - loss: 1.8542 - accuracy: 0.3199 - val_loss: 1.5776 - val_accuracy: 0.4301\n",
      "Epoch 2/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 1.5734 - accuracy: 0.4247 - val_loss: 1.3742 - val_accuracy: 0.4946\n",
      "Epoch 3/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 1.4618 - accuracy: 0.4682 - val_loss: 1.3034 - val_accuracy: 0.5267\n",
      "Epoch 4/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 1.3840 - accuracy: 0.5019 - val_loss: 1.3180 - val_accuracy: 0.5356\n",
      "Epoch 5/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 1.3153 - accuracy: 0.5280 - val_loss: 1.1624 - val_accuracy: 0.5830\n",
      "Epoch 6/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 1.2590 - accuracy: 0.5499 - val_loss: 1.3930 - val_accuracy: 0.5135\n",
      "Epoch 7/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 1.2126 - accuracy: 0.5689 - val_loss: 1.1823 - val_accuracy: 0.5937\n",
      "Epoch 8/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 1.1725 - accuracy: 0.5865 - val_loss: 1.0352 - val_accuracy: 0.6348\n",
      "Epoch 9/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 1.1374 - accuracy: 0.5972 - val_loss: 1.0253 - val_accuracy: 0.6404\n",
      "Epoch 10/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 1.1072 - accuracy: 0.6084 - val_loss: 0.9716 - val_accuracy: 0.6605\n",
      "Epoch 11/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 1.0762 - accuracy: 0.6196 - val_loss: 0.9642 - val_accuracy: 0.6615\n",
      "Epoch 12/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 1.0494 - accuracy: 0.6310 - val_loss: 0.9672 - val_accuracy: 0.6638\n",
      "Epoch 13/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 1.0336 - accuracy: 0.6385 - val_loss: 0.8968 - val_accuracy: 0.6896\n",
      "Epoch 14/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 1.0133 - accuracy: 0.6444 - val_loss: 0.8757 - val_accuracy: 0.6924\n",
      "Epoch 15/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.9943 - accuracy: 0.6542 - val_loss: 0.9261 - val_accuracy: 0.6829\n",
      "Epoch 16/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.9781 - accuracy: 0.6566 - val_loss: 0.8746 - val_accuracy: 0.6994\n",
      "Epoch 17/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.9616 - accuracy: 0.6638 - val_loss: 0.8714 - val_accuracy: 0.6992\n",
      "Epoch 18/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.9514 - accuracy: 0.6670 - val_loss: 0.8965 - val_accuracy: 0.6957\n",
      "Epoch 19/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.9360 - accuracy: 0.6731 - val_loss: 0.8654 - val_accuracy: 0.7007\n",
      "Epoch 20/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.9257 - accuracy: 0.6781 - val_loss: 0.9057 - val_accuracy: 0.6885\n",
      "Epoch 21/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.9137 - accuracy: 0.6817 - val_loss: 0.8115 - val_accuracy: 0.7199\n",
      "Epoch 22/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.9090 - accuracy: 0.6858 - val_loss: 0.8206 - val_accuracy: 0.7200\n",
      "Epoch 23/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.9038 - accuracy: 0.6866 - val_loss: 0.7856 - val_accuracy: 0.7294\n",
      "Epoch 24/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8919 - accuracy: 0.6942 - val_loss: 0.8433 - val_accuracy: 0.7092\n",
      "Epoch 25/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8862 - accuracy: 0.6936 - val_loss: 0.8226 - val_accuracy: 0.7280\n",
      "Epoch 26/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8827 - accuracy: 0.6964 - val_loss: 0.7532 - val_accuracy: 0.7400\n",
      "Epoch 27/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8782 - accuracy: 0.6973 - val_loss: 0.7773 - val_accuracy: 0.7344\n",
      "Epoch 28/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8726 - accuracy: 0.7000 - val_loss: 0.7553 - val_accuracy: 0.7474\n",
      "Epoch 29/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8655 - accuracy: 0.7018 - val_loss: 0.7364 - val_accuracy: 0.7511\n",
      "Epoch 30/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8634 - accuracy: 0.7062 - val_loss: 0.7550 - val_accuracy: 0.7426\n",
      "Epoch 31/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8567 - accuracy: 0.7068 - val_loss: 0.7637 - val_accuracy: 0.7413\n",
      "Epoch 32/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8505 - accuracy: 0.7080 - val_loss: 0.7578 - val_accuracy: 0.7469\n",
      "Epoch 33/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8441 - accuracy: 0.7115 - val_loss: 0.8131 - val_accuracy: 0.7263\n",
      "Epoch 34/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8489 - accuracy: 0.7085 - val_loss: 0.7513 - val_accuracy: 0.7401\n",
      "Epoch 35/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8419 - accuracy: 0.7124 - val_loss: 0.7675 - val_accuracy: 0.7488\n",
      "Epoch 36/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8374 - accuracy: 0.7134 - val_loss: 0.7217 - val_accuracy: 0.7555\n",
      "Epoch 37/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8368 - accuracy: 0.7126 - val_loss: 0.7660 - val_accuracy: 0.7410\n",
      "Epoch 38/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8278 - accuracy: 0.7167 - val_loss: 0.7252 - val_accuracy: 0.7527\n",
      "Epoch 39/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8339 - accuracy: 0.7160 - val_loss: 0.6894 - val_accuracy: 0.7664\n",
      "Epoch 40/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8249 - accuracy: 0.7192 - val_loss: 0.7744 - val_accuracy: 0.7384\n",
      "Epoch 41/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8228 - accuracy: 0.7200 - val_loss: 0.7013 - val_accuracy: 0.7625\n",
      "Epoch 42/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8165 - accuracy: 0.7230 - val_loss: 0.7736 - val_accuracy: 0.7447\n",
      "Epoch 43/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8173 - accuracy: 0.7244 - val_loss: 0.7083 - val_accuracy: 0.7582\n",
      "Epoch 44/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8168 - accuracy: 0.7218 - val_loss: 0.6850 - val_accuracy: 0.7636\n",
      "Epoch 45/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8181 - accuracy: 0.7228 - val_loss: 0.7642 - val_accuracy: 0.7422\n",
      "Epoch 46/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8138 - accuracy: 0.7251 - val_loss: 0.7767 - val_accuracy: 0.7476\n",
      "Epoch 47/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8116 - accuracy: 0.7270 - val_loss: 0.7413 - val_accuracy: 0.7590\n",
      "Epoch 48/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8051 - accuracy: 0.7294 - val_loss: 0.7086 - val_accuracy: 0.7597\n",
      "Epoch 49/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8054 - accuracy: 0.7274 - val_loss: 0.6770 - val_accuracy: 0.7733\n",
      "Epoch 50/100\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8019 - accuracy: 0.7290 - val_loss: 0.7355 - val_accuracy: 0.7534\n",
      "Epoch 51/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8050 - accuracy: 0.7270 - val_loss: 0.7317 - val_accuracy: 0.7572\n",
      "Epoch 52/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8079 - accuracy: 0.7271 - val_loss: 0.7130 - val_accuracy: 0.7635\n",
      "Epoch 53/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7982 - accuracy: 0.7310 - val_loss: 0.7007 - val_accuracy: 0.7703\n",
      "Epoch 54/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7966 - accuracy: 0.7327 - val_loss: 0.7634 - val_accuracy: 0.7442\n",
      "Epoch 55/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7964 - accuracy: 0.7312 - val_loss: 0.7063 - val_accuracy: 0.7636\n",
      "Epoch 56/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7889 - accuracy: 0.7346 - val_loss: 0.7983 - val_accuracy: 0.7427\n",
      "Epoch 57/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7933 - accuracy: 0.7305 - val_loss: 0.7125 - val_accuracy: 0.7609\n",
      "Epoch 58/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7943 - accuracy: 0.7332 - val_loss: 0.8507 - val_accuracy: 0.7273\n",
      "Epoch 59/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7922 - accuracy: 0.7344 - val_loss: 0.7668 - val_accuracy: 0.7525\n",
      "Epoch 60/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.7937 - accuracy: 0.7317 - val_loss: 0.6933 - val_accuracy: 0.7690\n",
      "Epoch 61/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.7836 - accuracy: 0.7360 - val_loss: 0.6652 - val_accuracy: 0.7800\n",
      "Epoch 62/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7948 - accuracy: 0.7355 - val_loss: 0.6858 - val_accuracy: 0.7725\n",
      "Epoch 63/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7854 - accuracy: 0.7337 - val_loss: 0.7654 - val_accuracy: 0.7443\n",
      "Epoch 64/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7932 - accuracy: 0.7337 - val_loss: 0.6816 - val_accuracy: 0.7777\n",
      "Epoch 65/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7895 - accuracy: 0.7363 - val_loss: 0.6615 - val_accuracy: 0.7775\n",
      "Epoch 66/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7905 - accuracy: 0.7340 - val_loss: 0.7120 - val_accuracy: 0.7678\n",
      "Epoch 67/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7913 - accuracy: 0.7337 - val_loss: 0.7149 - val_accuracy: 0.7635\n",
      "Epoch 68/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7923 - accuracy: 0.7367 - val_loss: 0.7180 - val_accuracy: 0.7608\n",
      "Epoch 69/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.7864 - accuracy: 0.7379 - val_loss: 0.6994 - val_accuracy: 0.7783\n",
      "Epoch 70/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7900 - accuracy: 0.7351 - val_loss: 0.7328 - val_accuracy: 0.7627\n",
      "Epoch 71/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.7913 - accuracy: 0.7348 - val_loss: 0.7638 - val_accuracy: 0.7464\n",
      "Epoch 72/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7858 - accuracy: 0.7376 - val_loss: 0.7294 - val_accuracy: 0.7610\n",
      "Epoch 73/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7924 - accuracy: 0.7354 - val_loss: 0.7409 - val_accuracy: 0.7517\n",
      "Epoch 74/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7941 - accuracy: 0.7362 - val_loss: 0.6730 - val_accuracy: 0.7733\n",
      "Epoch 75/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.7894 - accuracy: 0.7350 - val_loss: 0.7048 - val_accuracy: 0.7750\n",
      "Epoch 76/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7998 - accuracy: 0.7326 - val_loss: 0.7121 - val_accuracy: 0.7619\n",
      "Epoch 77/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.7976 - accuracy: 0.7336 - val_loss: 0.6623 - val_accuracy: 0.7772\n",
      "Epoch 78/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7944 - accuracy: 0.7344 - val_loss: 0.6907 - val_accuracy: 0.7718\n",
      "Epoch 79/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7974 - accuracy: 0.7344 - val_loss: 0.7524 - val_accuracy: 0.7439\n",
      "Epoch 80/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.7951 - accuracy: 0.7341 - val_loss: 0.7486 - val_accuracy: 0.7604\n",
      "Epoch 81/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.7918 - accuracy: 0.7342 - val_loss: 0.7421 - val_accuracy: 0.7527\n",
      "Epoch 82/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8054 - accuracy: 0.7345 - val_loss: 0.7200 - val_accuracy: 0.7634\n",
      "Epoch 83/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8031 - accuracy: 0.7328 - val_loss: 0.7166 - val_accuracy: 0.7616\n",
      "Epoch 84/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8063 - accuracy: 0.7306 - val_loss: 0.6907 - val_accuracy: 0.7756\n",
      "Epoch 85/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8063 - accuracy: 0.7359 - val_loss: 0.7571 - val_accuracy: 0.7560\n",
      "Epoch 86/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8005 - accuracy: 0.7350 - val_loss: 0.6941 - val_accuracy: 0.7703\n",
      "Epoch 87/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8029 - accuracy: 0.7349 - val_loss: 0.6578 - val_accuracy: 0.7809\n",
      "Epoch 88/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8015 - accuracy: 0.7344 - val_loss: 0.7072 - val_accuracy: 0.7709\n",
      "Epoch 89/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8032 - accuracy: 0.7337 - val_loss: 0.7396 - val_accuracy: 0.7559\n",
      "Epoch 90/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8042 - accuracy: 0.7347 - val_loss: 0.7321 - val_accuracy: 0.7595\n",
      "Epoch 91/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8058 - accuracy: 0.7320 - val_loss: 0.7242 - val_accuracy: 0.7603\n",
      "Epoch 92/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8075 - accuracy: 0.7342 - val_loss: 0.7146 - val_accuracy: 0.7605\n",
      "Epoch 93/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8020 - accuracy: 0.7344 - val_loss: 0.7987 - val_accuracy: 0.7374\n",
      "Epoch 94/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8093 - accuracy: 0.7320 - val_loss: 0.8248 - val_accuracy: 0.7303\n",
      "Epoch 95/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8067 - accuracy: 0.7326 - val_loss: 0.8169 - val_accuracy: 0.7367\n",
      "Epoch 96/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8029 - accuracy: 0.7354 - val_loss: 0.7433 - val_accuracy: 0.7577\n",
      "Epoch 97/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8044 - accuracy: 0.7352 - val_loss: 0.7596 - val_accuracy: 0.7529\n",
      "Epoch 98/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8132 - accuracy: 0.7318 - val_loss: 0.7487 - val_accuracy: 0.7538\n",
      "Epoch 99/100\n",
      "1563/1563 [==============================] - 15s 10ms/step - loss: 0.8191 - accuracy: 0.7294 - val_loss: 0.6698 - val_accuracy: 0.7719\n",
      "Epoch 100/100\n",
      "1563/1563 [==============================] - 16s 10ms/step - loss: 0.8153 - accuracy: 0.7316 - val_loss: 0.6642 - val_accuracy: 0.7830\n",
      "Saved trained model at /ssd/pyr/AI_HomeWork/四次实验/实验四_十分类/saved_models/keras_cifar10_trained_model.h5 \n",
      "10000/10000 [==============================] - 1s 95us/step\n",
      "Test loss: 0.6642301881790161\n",
      "Test accuracy: 0.7829999923706055\n"
     ]
    }
   ],
   "source": [
    "from __future__ import print_function\n",
    "import keras\n",
    "from keras.datasets import cifar10\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout, Activation, Flatten\n",
    "from keras.layers import Conv2D, MaxPooling2D\n",
    "import os\n",
    "\n",
    "batch_size = 32\n",
    "num_classes = 10\n",
    "epochs = 100\n",
    "data_augmentation = True\n",
    "num_predictions = 20\n",
    "save_dir = os.path.join(os.getcwd(), 'saved_models')\n",
    "model_name = 'keras_cifar10_trained_model.h5'\n",
    "\n",
    "# The data, split between train and test sets:\n",
    "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n",
    "print('x_train shape:', x_train.shape)\n",
    "print(x_train.shape[0], 'train samples')\n",
    "print(x_test.shape[0], 'test samples')\n",
    "\n",
    "# Convert class vectors to binary class matrices.\n",
    "y_train = keras.utils.to_categorical(y_train, num_classes)\n",
    "y_test = keras.utils.to_categorical(y_test, num_classes)\n",
    "\n",
    "model = Sequential()\n",
    "model.add(Conv2D(32, (3, 3), padding='same',\n",
    "                 input_shape=x_train.shape[1:]))\n",
    "model.add(Activation('relu'))\n",
    "model.add(Conv2D(32, (3, 3)))\n",
    "model.add(Activation('relu'))\n",
    "model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "model.add(Dropout(0.25))\n",
    "\n",
    "model.add(Conv2D(64, (3, 3), padding='same'))\n",
    "model.add(Activation('relu'))\n",
    "model.add(Conv2D(64, (3, 3)))\n",
    "model.add(Activation('relu'))\n",
    "model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "model.add(Dropout(0.25))\n",
    "\n",
    "model.add(Flatten())\n",
    "model.add(Dense(512))\n",
    "model.add(Activation('relu'))\n",
    "model.add(Dropout(0.5))\n",
    "model.add(Dense(num_classes))\n",
    "model.add(Activation('softmax'))\n",
    "\n",
    "# initiate RMSprop optimizer\n",
    "opt = keras.optimizers.RMSprop(learning_rate=0.0001, decay=1e-6)\n",
    "\n",
    "# Let's train the model using RMSprop\n",
    "model.compile(loss='categorical_crossentropy',\n",
    "              optimizer=opt,\n",
    "              metrics=['accuracy'])\n",
    "\n",
    "x_train = x_train.astype('float32')\n",
    "x_test = x_test.astype('float32')\n",
    "x_train /= 255\n",
    "x_test /= 255\n",
    "\n",
    "if not data_augmentation:\n",
    "    print('Not using data augmentation.')\n",
    "    model.fit(x_train, y_train,\n",
    "              batch_size=batch_size,\n",
    "              epochs=epochs,\n",
    "              validation_data=(x_test, y_test),\n",
    "              shuffle=True)\n",
    "else:\n",
    "    print('Using real-time data augmentation.')\n",
    "    # This will do preprocessing and realtime data augmentation:\n",
    "    datagen = ImageDataGenerator(\n",
    "        featurewise_center=False,  # set input mean to 0 over the dataset\n",
    "        samplewise_center=False,  # set each sample mean to 0\n",
    "        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n",
    "        samplewise_std_normalization=False,  # divide each input by its std\n",
    "        zca_whitening=False,  # apply ZCA whitening\n",
    "        zca_epsilon=1e-06,  # epsilon for ZCA whitening\n",
    "        rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)\n",
    "        # randomly shift images horizontally (fraction of total width)\n",
    "        width_shift_range=0.1,\n",
    "        # randomly shift images vertically (fraction of total height)\n",
    "        height_shift_range=0.1,\n",
    "        shear_range=0.,  # set range for random shear\n",
    "        zoom_range=0.,  # set range for random zoom\n",
    "        channel_shift_range=0.,  # set range for random channel shifts\n",
    "        # set mode for filling points outside the input boundaries\n",
    "        fill_mode='nearest',\n",
    "        cval=0.,  # value used for fill_mode = \"constant\"\n",
    "        horizontal_flip=True,  # randomly flip images\n",
    "        vertical_flip=False,  # randomly flip images\n",
    "        # set rescaling factor (applied before any other transformation)\n",
    "        rescale=None,\n",
    "        # set function that will be applied on each input\n",
    "        preprocessing_function=None,\n",
    "        # image data format, either \"channels_first\" or \"channels_last\"\n",
    "        data_format=None,\n",
    "        # fraction of images reserved for validation (strictly between 0 and 1)\n",
    "        validation_split=0.0)\n",
    "\n",
    "    # Compute quantities required for feature-wise normalization\n",
    "    # (std, mean, and principal components if ZCA whitening is applied).\n",
    "    datagen.fit(x_train)\n",
    "\n",
    "    # Fit the model on the batches generated by datagen.flow().\n",
    "    model.fit_generator(datagen.flow(x_train, y_train,\n",
    "                                     batch_size=batch_size),\n",
    "                        epochs=epochs,\n",
    "                        validation_data=(x_test, y_test),\n",
    "                        workers=4)\n",
    "\n",
    "# Save model and weights\n",
    "if not os.path.isdir(save_dir):\n",
    "    os.makedirs(save_dir)\n",
    "model_path = os.path.join(save_dir, model_name)\n",
    "model.save(model_path)\n",
    "print('Saved trained model at %s ' % model_path)\n",
    "\n",
    "# Score trained model.\n",
    "scores = model.evaluate(x_test, y_test, verbose=1)\n",
    "print('Test loss:', scores[0])\n",
    "print('Test accuracy:', scores[1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
